package com.jsp.servlet;


import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;






import javax.servlet.http.HttpSession;

import com.jsp.bean.UserBean;
import com.jsp.service.UserService;

public class UserServlet extends HttpServlet {
	
	
private static final long serialVersionUID =1L;
	
    private UserService userService;
    private UserBean user;

//	дservletʱ�̳���HttpServlet��
//	ctrl+s����һ�£�Ȼ��ѡ���������UserServlet��	
//	�һ���꣬Ȼ���Override/Implment Methods,
//	ѡ��doGet��doPost�㣬�Զ����
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		response.setCharacterEncoding("UTF-8");
		
		String mark = request.getParameter("mark");
	
		if("register".equals(mark)){
			try {
				register(request,response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if("login".equals(mark)){
			try {
				login(request,response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
	
	private void login(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
		
		String name = request.getParameter("name");		
		String password = request.getParameter("password");
		HttpSession session=request.getSession();
		session.setAttribute("name", name);
		userService=new UserService();
		user = userService.login(name,password);
		System.out.println("sss"+name);
		System.out.println("session"+session.getAttribute("name"));
		if(null!=user){
			
			request.getRequestDispatcher("../users/success.jsp").forward(request, response);	
		}else{

			request.getRequestDispatcher("../users/error.jsp").forward(request, response);
		}
	}
	
	
	private void register(HttpServletRequest request,
			HttpServletResponse response) throws SQLException, ServletException, IOException {
		// TODO Auto-generated method stub
				
				String name = request.getParameter("name");				
				String password = request.getParameter("password");				
				String name1 = request.getParameter("name1");				
				String address = request.getParameter("address");
				String idcard = request.getParameter("idcard");
				String number = request.getParameter("number");
				String job = request.getParameter("job");
				 userService = new UserService();
				user = userService.register(name,password,name1,number,address,idcard,job);
				
				if(null!=user){
					
					request.getRequestDispatcher("../users/registersuccess.jsp").forward(request, response);	
				}else{
					request.getRequestDispatcher("../users/registererror.jsp").forward(request, response);
				}
		
	}
	
}
